#! /usr/bin/env python

# Copyright (c) 2011 - 2012 Tony Bai <bigwhite.cn@gmail.com>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import os
from build_utils import env
from build_utils import config
from build_utils import command
from build_utils import errnos
from build_utils import options
from build_utils import core
from build_utils import pack

def buildc_run():
    buildc_home = os.path.dirname(os.path.realpath(__file__))
    opts, args = options.options_parse()

    if args[0] == 'init':
        core.init(buildc_home)
        sys.exit(0)

    if args[0] == 'cache':
        if args[1] == 'init':
            core.cache_init(opts.cmode)
            sys.exit(0)

        if args[1] == 'update':
            core.cache_update(opts.cmode)
            sys.exit(0)

        if args[1] == 'remove':
            core.cache_remove()
            sys.exit(0)

        print "Error: command [cache] does not support arg [%s]" % args[1]
        sys.exit(errnos.errors['args_invalid'])

    if args[0] == 'config-make':
        core.config_make(buildc_home, opts.cmode)
        sys.exit(0)

    if args[0] == 'pack':
        if args[1] == 'create':
            pack.pack_create(buildc_home, opts)
            sys.exit(0)

        if args[1] == 'build':
            pack.pack_build(opts)
            sys.exit(0)

        if args[1] == 'clean':
            pack.pack_clean()
            sys.exit(0)

        if args[1] == 'upload':
            pack.pack_upload(opts)
            sys.exit(0)

        print "Error: command [pack] does not support arg [%s]" % args[1]
        sys.exit(errnos.errors['args_invalid'])

if __name__ == '__main__':
    buildc_run()
